package com.bootdo.common.utils;

import com.bootdo.system.domain.ProductInnerDO;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class MyUtils {
    public static Double getDouble2(Double num) {
        if (num == null) {
            return null;
        }
        if (num.isNaN()) {
            return null;
        }
        num = num.doubleValue();
        if (num.isInfinite()) {
            return null;
        }

        BigDecimal b = new BigDecimal(num);
        double result = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();



        return result;
    }


    public static String getAlldouble(Double num){

        BigDecimal bigDecimal = new BigDecimal(num);

        String result = bigDecimal.toString();

        DecimalFormat df2 = new DecimalFormat("#.##");

        return df2.format(bigDecimal);
    }

    public static ProductInnerDO buildMoney(ProductInnerDO productInnerDO) {

        Integer proNum = productInnerDO.getProNum();

        double allanzhuangchenben = productInnerDO.getProductDO().getAnzhuangChengben() ;//安装成本
        allanzhuangchenben = getDouble2(allanzhuangchenben);
        System.out.println("单个安装成本="+allanzhuangchenben);
        double allshebeicb = productInnerDO.getProductDO().getShebeiChengben() ;//设备
        allshebeicb = getDouble2(allshebeicb);
        System.out.println("单个设备成本="+allshebeicb);
        double chanpincb = productInnerDO.getProductDO().getChengben();
        chanpincb = getDouble2(chanpincb);
        System.out.println("单个产品成本="+chanpincb);
        double allchengben = allanzhuangchenben + allshebeicb + chanpincb;
        allchengben = getDouble2(allchengben);
        System.out.println("单个总成本="+allchengben);
        Double pronum2 = new Double(proNum);
        Double allchengben2 = allchengben * pronum2;
        System.out.println("all成本="+allchengben2);
        if(allchengben2!=null){
            allchengben2 = MyUtils.getDouble2(allchengben2);
            productInnerDO.setAllchengben(allchengben2);
        }



        double allbaojia = productInnerDO.getBaojia() * proNum;
        allbaojia = getDouble2(allbaojia);

        productInnerDO.setAllbaojia(allbaojia);

        Double alllirun = allbaojia - allchengben2;
        alllirun = getDouble2(alllirun);

        if (alllirun != null) {
            productInnerDO.setAlllirun(alllirun);
        }


        Double alllirunbi = alllirun / allchengben2 * 100;

        if (alllirun != null){
            alllirunbi = getDouble2(alllirunbi);
            productInnerDO.setProfit(alllirunbi);
        }





        Double profit = getDouble2(productInnerDO.getProfit());
        if (profit != null) {
            productInnerDO.setProfit(profit);
        }


        return productInnerDO;
    }
}
